package com.henry.chatkit.messages

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.henry.chatkit.DataChangeType
import com.henry.chatkit.MessageType
import com.henry.chatkit.OnCustomMessageDrawListener
import com.henry.chatkit.OnItemClickListener
import com.henry.chatkit.messages.holder.MessageBaseHolder
import com.henry.chatkit.messages.holder.MessageContentHolder
import com.henry.chatkit.messages.holder.MessageCustomHolder
import com.henry.chatkit.messages.holder.MessageHeaderHolder
import com.henry.chatkit.utils.BackgroundTasks


open class MessageListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var mDataSource = mutableListOf<MessageInfo>()
    private var mLoading = true

    private var mRecycleView: MessageLayout? = null
    lateinit var mOnItemClickListener: OnItemClickListener
    private var mOnCustomMessageDrawListener: OnCustomMessageDrawListener? = null

    open fun getOnItemClickListener(): OnItemClickListener {
        return mOnItemClickListener
    }

    open fun setOnItemClickListener(listener: OnItemClickListener) {
        mOnItemClickListener = listener
    }

    open fun setOnCustomMessageDrawListener(listener: OnCustomMessageDrawListener) {
        mOnCustomMessageDrawListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val holder = MessageFactory.getInstance(parent, this, viewType)
        return holder!!
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val msg: MessageInfo? = getItem(position)
        val baseHolder: MessageBaseHolder = holder as MessageBaseHolder
        baseHolder.setOnItemClickListener(mOnItemClickListener)

        when (getItemViewType(position)) {
            MessageType.HEADER_VIEW -> {
                (baseHolder as MessageHeaderHolder).setLoadingStatus(mLoading)
            }
        }
        baseHolder.layoutViews(msg, position)

        // 对于自定义消息，需要在正常布局之后，交给外部调用者重新加载渲染
        if (getItemViewType(position) == MessageType.CUSTOM) {
            val customHolder = holder as MessageCustomHolder
            mOnCustomMessageDrawListener?.onDraw(customHolder, msg)
        }
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        mRecycleView = recyclerView as MessageLayout

    }

    override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
        if (holder is MessageContentHolder) {
            (holder as MessageContentHolder).msgContentFrame.background = null
        }
    }

    override fun getItemCount(): Int {
        return mDataSource.size + 1
    }

    override fun getItemViewType(position: Int): Int {
        if (position == 0) {
            return MessageType.HEADER_VIEW
        }
        val msg: MessageInfo? = getItem(position)
        if (msg != null) {
            return msg.msgType
        }
        return 0
    }

    open fun setDataSource(provider: IMessageProvider?) {
        if (provider == null) {
            mDataSource.clear()
        } else {
            mDataSource = provider.getDataSource()
            provider.setAdapter(this)
        }
        notifyDataSourceChanged(DataChangeType.REFRESH, itemCount)
    }

    open fun getItem(position: Int): MessageInfo? {
        return if (position == 0 || mDataSource.isEmpty()) {
            null
        } else mDataSource[position - 1]
    }

    open fun showLoading() {
        if (mLoading) {
            return
        }
        mLoading = true
        notifyItemChanged(0)
    }

    fun notifyDataSourceChanged(type: Int, value: Int) {
        BackgroundTasks.getInstance()?.postDelayed(Runnable {
            mLoading = false
            if (type == DataChangeType.REFRESH) {
                notifyDataSetChanged()
                mRecycleView?.scrollToEnd()
            } else if (type == DataChangeType.ADD_BACK) {
                notifyItemRangeInserted(mDataSource.size + 1, value)
                mRecycleView?.scrollToEnd()
            } else if (type == DataChangeType.UPDATE) {
                notifyItemChanged(value + 1)
            } else if (type == DataChangeType.LOAD || type == DataChangeType.ADD_FRONT) {
                //加载条目为数0，只更新动画
                if (value == 0) {
                    notifyItemChanged(0)
                } else {
                    //加载过程中有可能之前第一条与新加载的最后一条的时间间隔不超过5分钟，时间条目需去掉，所以这里的刷新要多一个条目
                    if (itemCount > value) {
                        notifyItemRangeInserted(0, value)
                    } else {
                        notifyItemRangeInserted(0, value)
                    }
                }
            } else if (type == DataChangeType.DELETE) {
                notifyItemRemoved(value + 1)
                notifyDataSetChanged()
                mRecycleView?.scrollToEnd()
            }
        }, 100)
    }

}